#include <iostream>
using namespace std;

class DirectedGraph
{
private:
    int vertexCount;
    int** adjacencyMatrix;
    int* visited;
    int getNextUnvisitedNeighbor(int vertex);
    void depthFirstSearch(int startVertex);

public:
    DirectedGraph();
    ~DirectedGraph();
    void traverseDepthFirst();
};

DirectedGraph::DirectedGraph()
{
    int i, j;
    cin >> vertexCount;
    adjacencyMatrix = new int* [vertexCount];
    visited = new int[vertexCount];
    for (i = 0; i < vertexCount; i++)
    {
        adjacencyMatrix[i] = new int[vertexCount];
        visited[i] = 0;
        for (j = 0; j < vertexCount; j++)
            cin >> adjacencyMatrix[i][j];
    }
}

DirectedGraph::~DirectedGraph()
{
    for (int i = 0; i < vertexCount; i++)
        delete[] adjacencyMatrix[i];
    delete[] visited;
}

int DirectedGraph::getNextUnvisitedNeighbor(int vertex)
{
    for (int i = 0; i < vertexCount; i++)
    {
        if (adjacencyMatrix[vertex][i] && !visited[i])
            return i;
    }
    return -1;
}

void DirectedGraph::depthFirstSearch(int startVertex)
{
    visited[startVertex] = 1;
    int nextVertex = getNextUnvisitedNeighbor(startVertex);
    while (nextVertex + 1)
    {
        depthFirstSearch(nextVertex);
        nextVertex = getNextUnvisitedNeighbor(startVertex);
    }
}

void DirectedGraph::traverseDepthFirst()
{
    int i, j, k, componentCount;
    int isNotStronglyConnected = 0;
    int position = 0;

    for (j = 0; j < vertexCount; j++)
    {
        componentCount = 0;
        k = j;
        for (i = 0; i < vertexCount; i++)
        {
            if (!visited[k])
            {
                depthFirstSearch(k);
                componentCount++;
            }
            k = (k + 1) % vertexCount;
        }

        if (componentCount != 1)
            isNotStronglyConnected = 1;

        for (i = 0; i < vertexCount; i++)
            visited[i] = 0;
    }

    if (isNotStronglyConnected)
        cout << "No" << endl;
    else
        cout << "Yes" << endl;
}

int main()
{
    int testCases;
    cin >> testCases;

    while (testCases--)
    {
        DirectedGraph myGraph;
        myGraph.traverseDepthFirst();
    }

    return 0;
}
